/*
* VFS_Group 19 - IIIT-B, MTech Program, 1st Semester, Data Management.
*
* Header file having prototype of structure for NArray List and all operations
* on it. NArrayTree
*
*/
#ifndef STANDARD_IO
#define STANDARD_IO

#include<stdio.h>

#endif

#ifndef STANDARD_LIB
#define STANDARD_LIB

#include<stdlib.h>

#endif

#ifndef STRING_LIB
#define STRING_LIB

#include<string.h>
#include "../include/string_operations.h"
typedef char** STRINGARRAY;
typedef char* STRING;

#endif

//#include "../include/custom_data_types.h"



STRINGARRAY GetSplittedPath(STRING filepath, char splitKey)
{
	int stringLength = strlen(filepath);
	int stringIndex = 0;
	int depth = 0;
	int i = 0;
	int fileNestingDepth = GetFileNestingDepth(filepath);

	STRINGARRAY splittedPaths = (STRINGARRAY)malloc(sizeof(STRING) * (fileNestingDepth + 1)); //Maximum depth allowed.
	STRING tempString = (STRING)malloc(sizeof(char) * 256);

	for(i = 0; i< stringLength; i++)
	{
		if(filepath[i] != '\\')
		{
			tempString[stringIndex] = filepath[i];
			stringIndex = stringIndex + 1;
		}
		else
		{
			tempString[stringIndex] = '\0';
			splittedPaths[depth] = (STRING)malloc(sizeof(char)*strlen(tempString));
			strcpy(splittedPaths[depth], tempString);
			depth = depth + 1;

			// Restart storing next directory name.
			stringIndex = 0;
			free(tempString);
			tempString = (STRING)malloc(sizeof(STRING) * 256);
		}
	}

	// Copy last file name.
	tempString[stringIndex] = '\0';
	splittedPaths[depth] = (STRING)malloc(sizeof(char)*strlen(tempString));
	strcpy(splittedPaths[depth], tempString);

	// Free Any Resources temporarily created.
	free(tempString);

	return splittedPaths;
}

int GetFileNestingDepth(STRING filepath)
{
	int fileNestingDepth = 0;
	int stringLength = 0;
	int i = 0;

	stringLength = strlen(filepath);
	for(i = 0; i < stringLength; i++)
	{
		if(filepath[i] == '\\')
		{
			fileNestingDepth = fileNestingDepth + 1;
		}
	}

	return fileNestingDepth;
}
